package base.utils

import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Build
import android.widget.ImageView
import base.sys.*
import com.blankj.utilcode.util.ActivityUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.tange.ai.iot.client.gnssa.R
import java.io.File
import java.math.BigDecimal


/**
 * @author njmsir
 * Created by njmsir on 2017/10/14.
 */
object ImageUtils {

    private var shopOptions: RequestOptions? = null

    fun setLoadPic(drawable: Drawable) {
        shopOptions = RequestOptions()
            .placeholder(drawable)
            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
    }




    val options = RequestOptions()
        .centerCrop()
        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)

    val nomalOptions = RequestOptions()
        .centerCrop()
        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)

    private val bannerOptions = RequestOptions()
        .centerCrop()
        .placeholder(R.mipmap.ic_launcher)
        .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)



    val productDetail = RequestOptions()
        .centerCrop()
        .placeholder(R.mipmap.ic_launcher)
        .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)


    fun loadImage(url: String?, imageView: ImageView) {
        Glide.with(imageView.context).load((url ?: "") + PRODUCT_IMAGE_WEBP).apply(options)
            .into(imageView)
    }


    /** * 获取指定文件夹内所有文件大小的和 * * @param file file * @return size * @throws Exception  */
    @Throws(Exception::class)
    fun getFolderSize(file: File): Long {
        var size: Long = 0
        try {
            val fileList = file.listFiles()
            for (aFileList in fileList!!) {
                size += if (aFileList.isDirectory) {
                    getFolderSize(aFileList)
                } else {
                    aFileList.length()
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return size
    }


    /** * 格式化单位 * * @param size size * @return size */
    fun getFormatSize(size: Double): String {
        val kiloByte = size / 1024
        if (kiloByte < 1) {
            return size.toString() + "Byte"
        }

        val megaByte = kiloByte / 1024
        if (megaByte < 1) {
            val result1 = BigDecimal(java.lang.Double.toString(kiloByte))
            return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB"
        }

        val gigaByte = megaByte / 1024
        if (gigaByte < 1) {
            val result2 = BigDecimal(java.lang.Double.toString(megaByte))
            return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB"
        }

        val teraBytes = gigaByte / 1024
        if (teraBytes < 1) {
            val result3 = BigDecimal(java.lang.Double.toString(gigaByte))
            return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB"
        }
        val result4 = BigDecimal(teraBytes)

        return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB"
    }

    fun displayBgForDrawer(url: Int?, imageView: ImageView, activity: Activity) {
        var url = url

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
            if (!activity.isDestroyed) {
                Glide.with(imageView.context).load(url)
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            }
        } else{
            Glide.with(imageView.context).load(url)
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                .into(imageView)
        }
    }

    fun displayIconForDrawer(url: Int?, imageView: ImageView, activity: Activity) {
        var url = url
        if (!activity.isDestroyed) {
            Glide.with(imageView.context).load(url)
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions).into(imageView)
        }
    }

    fun displayProductDetailImageToWebp(url: String?, imageView: ImageView, activity: Activity) {
        var url = url
        if (url != null) {
            if (url.contains("http")) {
                url += "?x-oss-process=image/format,webp"
            }
        }
        if (!activity.isDestroyed) {
            Glide.with(imageView?.context!!).load(url)
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions).into(imageView)
        }
    }

    fun displayProductDetailImageToWebpV2(url: String?, imageView: ImageView) {
        if (ActivityUtils.getTopActivity() == null || ActivityUtils.getTopActivity().isDestroyed) return

        if (url != null) {
            if (url.contains("http")) {
                if (url.contains("?x-oss-process=image")) {
                    Glide.with(imageView.context).load(url + PRODUCT_DETAIL_IMAGE_TO_WEBP)
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                } else {
                    Glide.with(imageView.context).load(url + PRODUCT_DETAIL_IMAGE_TO_WEBP)
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                }
            } else {
                Glide.with(imageView.context).load(url)
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            }
        } else {
            Glide.with(imageView.context).load("")
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions).into(imageView)
        }
    }


    fun displayImage(url: String, imageView: ImageView, activity: Activity) {
        if (!activity.isDestroyed) {
            Glide.with(imageView.context).load(url + PRODUCT_IMAGE)
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                .into(imageView)
        }
    }

    fun displayImageWebp256(url: String?, imageView: ImageView, activity: Activity?) {
        if (activity != null) {
            if (!activity.isDestroyed) {
                if (url != null) {
                    Glide.with(imageView.context).load(url + PRODUCT_IMAGE_WEBP)
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                } else {
                    Glide.with(imageView.context).load("")
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                }
            }
        } else {
            if (url != null) {
                try {
                    Glide.with(ApplicationContext.mContext).load(url + PRODUCT_IMAGE_WEBP)
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                } catch (e: Exception) {

                }
            } else {
                try {
                    Glide.with(ApplicationContext.mContext).load("").apply(bannerOptions)
                        .into(imageView)
                } catch (e: Exception) {

                }
            }
        }
    }

    fun displayImageWebp256V2(url: String?, imageView: ImageView, activity: Activity?) {
        if (ActivityUtils.getTopActivity() == null || ActivityUtils.getTopActivity().isDestroyed) return
        if (activity != null) {
            if (!activity.isDestroyed) {
                if (url != null) {
                    if (url.contains("http")) {
                        Glide.with(imageView.context).load(url + PRODUCT_IMAGE_WEBP_V2)
                            .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                            .into(imageView)
                    } else {
                        Glide.with(imageView.context).load(BASE_URL + url + PRODUCT_IMAGE_WEBP_V2)
                            .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                            .into(imageView)
                    }
                } else {
                    Glide.with(imageView.context).load("")
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                }
            }
        } else {
            Glide.with(ApplicationContext.mContext).load("")
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions).into(imageView)
        }
    }

    fun displayImageWebp256V2(url: String?, imageView: ImageView) {
        if (ActivityUtils.getTopActivity() == null || ActivityUtils.getTopActivity().isDestroyed) return
        if (url != null) {
            if (url.contains("http")) {
                Glide.with(imageView.context).load(/*BASE_URL + */url + PRODUCT_IMAGE_WEBP_V2)
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            } else {
                Glide.with(imageView.context).load(BASE_URL + url + PRODUCT_IMAGE_WEBP_V2)
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            }
        } else {
            Glide.with(imageView.context).load("")
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions).into(imageView)
        }
    }

    fun displayImageV2(url: String?, imageView: ImageView) {
        if (ActivityUtils.getTopActivity() == null || ActivityUtils.getTopActivity().isDestroyed) return
        if (url != null) {
            if (url.contains("http")) {
                Glide.with(imageView.context).load(url)
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            } else {
                Glide.with(imageView.context).load(url)
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            }
        } else {
            Glide.with(imageView.context).load("")
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions).into(imageView)
        }
    }

    fun displayImageWebp256V2(url: Int?, imageView: ImageView) {
        if (ActivityUtils.getTopActivity() == null || ActivityUtils.getTopActivity().isDestroyed) return
        if (url != null) {
            Glide.with(imageView.context).load(url)
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions).into(imageView)
        } else {
            Glide.with(imageView.context).load("")
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions).into(imageView)
        }
    }

    fun displayImage512(url: String?, imageView: ImageView) {
        if (ActivityUtils.getTopActivity() == null || ActivityUtils.getTopActivity().isDestroyed) return

        if (url != null) {
            if (url.contains("http")) {
                if (url.contains("?x-oss-process=image")) {
                    Glide.with(imageView.context).load(url + PRODUCT_IMAGE256_V2)
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                } else {
                    Glide.with(imageView.context).load(url + PRODUCT_IMAGE256)
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                }
            } else {
                Glide.with(imageView.context).load(url)
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            }
        } else {
            Glide.with(imageView.context).load("")
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions).into(imageView)
        }
    }


    fun displayImage128(url: String?, imageView: ImageView, activity: Activity) {
        if (ActivityUtils.getTopActivity() == null || ActivityUtils.getTopActivity().isDestroyed) return
        if (!activity.isDestroyed) {
            if (url != null) {
                Glide.with(imageView.context).load(url + PRODUCT_IMAGE128)
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            } else {
                Glide.with(imageView.context).load("")
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            }
        }
    }

    fun displayProductDetailImage(url: String?, imageView: ImageView, activity: Activity) {
        if (!activity.isDestroyed) {
            if (url != null) {
                Glide.with(imageView.context).load(url)
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            } else {
                Glide.with(imageView.context).load("")
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            }
        }
    }

    fun displayImage1024(
        url: String?,
        imageView: ImageView,
        mContext: Context,
        activity: Activity
    ) {
        if (!activity.isDestroyed) {
            if (url != null) {
                Glide.with(mContext).load(url + PRODUCT_IMAGE1024)
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            } else {
                Glide.with(mContext).load("")
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            }
        }
    }

    fun displayImage1024V2(url: String?, imageView: ImageView, activity: Activity?) {
        if (activity != null) {
            if (!activity.isDestroyed) {
                if (url != null) {
                    if (url.contains("http")) {
                        if (url.contains("?x-oss-process=image")) {
                            Glide.with(imageView.context).load(url + PRODUCT_IMAGE1024_V2)
                                .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                                .into(imageView)
                        } else {
                            Glide.with(imageView.context).load(url + PRODUCT_IMAGE1024)
                                .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                                .into(imageView)
                        }
                    } else {
                        Glide.with(imageView.context).load(BASE_URL + url + PRODUCT_IMAGE1024)
                            .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                            .into(imageView)
                    }
                } else {
                    Glide.with(imageView.context).load("")
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                }
            }
        } else {
            Glide.with(ApplicationContext.mContext).load("")
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions).into(imageView)
        }
    }


    fun displayImage2048V2(url: String?, imageView: ImageView, activity: Activity?) {
        if (activity != null) {
            if (!activity.isDestroyed) {
                if (url != null) {
                    if (url.contains("http")) {
                        if (url.contains("?x-oss-process=image")) {
                            Glide.with(imageView.context).load(url + PRODUCT_IMAGE2048_V2)
                                .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                                .into(imageView)
                        } else {
                            Glide.with(imageView.context).load(url + PRODUCT_IMAGE2048)
                                .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                                .into(imageView)
                        }
                    } else {
                        Glide.with(imageView.context).load(BASE_URL + url + PRODUCT_IMAGE2048)
                            .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                            .into(imageView)
                    }
                } else {
                    Glide.with(imageView.context).load("")
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                }
            }
        } else {
            Glide.with(ApplicationContext.mContext).load("")
                .apply(if (shopOptions != null) shopOptions!! else bannerOptions).into(imageView)
        }
    }


    fun displayImage1024V2(
        url: String?,
        imageView: ImageView,
        mContext: Context,
        activity: Activity
    ) {
        if (!activity.isDestroyed) {
            if (url != null) {
                if (url.contains("http")) {
                    Glide.with(mContext).load(url + PRODUCT_IMAGE1024_V2)
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                } else {
                    Glide.with(mContext).load(BASE_URL + url + PRODUCT_IMAGE1024_V2)
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                }
            } else {
                Glide.with(mContext).load("")
                    .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                    .into(imageView)
            }
        }
    }

    fun displayDesignQrImage(
        url: String?,
        imageView: ImageView,
        activity: Activity
    ) {
        if (!activity.isDestroyed) {
            if (url != null) {
                if (url.contains("http")) {
                    Glide.with(activity).load(url)
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                } else {
                    Glide.with(activity).load(BASE_URL + url)
                        .apply(if (shopOptions != null) shopOptions!! else bannerOptions)
                        .into(imageView)
                }
            } else {
                Glide.with(activity).load("").apply(bannerOptions).into(imageView)
            }
        }
    }


}